//
//  PhotoBrowerCollectionViewCell.swift
//  JIMWeiBo
//
//  Created by 陈恒均 on 2018/2/24.
//  Copyright © 2018年 陈恒均. All rights reserved.
//

import UIKit
import SDWebImage

protocol  PhotoBrowerCollectionViewCellDelegate {
    func imageViewClick()
}

class PhotoBrowerCollectionViewCell: UICollectionViewCell {
    lazy var imageView : UIImageView = UIImageView()
    // MARK:- 懒加载属性
    private lazy var scrollView : UIScrollView = UIScrollView()
    
    private lazy var progressView : ProgressView = ProgressView()
    
    
    var delegate : PhotoBrowerCollectionViewCellDelegate?
    
    var picURL : NSURL?{
        didSet{
            setupContent(picURL: picURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


// MARK:- 设置UI
extension PhotoBrowerCollectionViewCell{
    private func setupUI(){
        addSubview(scrollView)
        addSubview(imageView)
        addSubview(progressView)
        
        
        scrollView.frame = contentView.bounds
        scrollView.frame.size.width -= 20
//        scrollView.isUserInteractionEnabled = false
        progressView.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
        
        progressView.center = CGPoint(x: UIScreen.main.bounds.width * 0.5, y: UIScreen.main.bounds.height * 0.5)
        progressView.isHidden = true
        progressView.backgroundColor = UIColor.clear
        
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(PhotoBrowerCollectionViewCell.imageClick))
        imageView.addGestureRecognizer(tapGes)
        imageView.isUserInteractionEnabled = true
        
        
    }
}
// MARK:- 时间监听
extension PhotoBrowerCollectionViewCell{
    @objc private func imageClick(){
//        print("0-----")
        delegate?.imageViewClick()
    }
}

// MARK:- 方法
extension PhotoBrowerCollectionViewCell{
    private func setupContent(picURL : NSURL?){
        guard let picURL = picURL else {
            return
        }
        guard let image = SDWebImageManager.shared().imageCache?.imageFromCache(forKey: picURL.absoluteString) else{
            return
        }
        
        let width  = UIScreen.main.bounds.width
        let height = width *  image.size.height / image.size.width
        var y : CGFloat = 0
        if height > UIScreen.main.bounds.height {
            y = 0
        }else{
            y = (UIScreen.main.bounds.height - height) * 0.5
        }
        imageView.frame = CGRect(x: 0, y: y, width: width, height: height)
        
        
        progressView.isHidden = true
        imageView.sd_setImage(with: getBigURL(smallURL: picURL) as URL, placeholderImage: image, options: [], progress: { (current, total, _) in
            self.progressView.progress = CGFloat(current) / CGFloat(total)
        }) { (_, _, _, _) in
            self.progressView.isHidden = true
        }        
        scrollView.contentSize = CGSize(width: 0, height: height)
    }
    private func getBigURL(smallURL : NSURL) -> NSURL{
        let smallURLString = smallURL.absoluteString
        let bigURLString = (smallURLString! as NSString).replacingOccurrences(of: "thumbnail", with: "bmiddle")
        return NSURL(string: bigURLString)!
        
        
    }
}
